/**
 * 
 */
package info.sswap.ontologies.modularity.repo.rdf;

import java.io.File;

import org.openrdf.model.vocabulary.RDF;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.config.RepositoryConfig;
import org.openrdf.repository.manager.LocalRepositoryManager;
import org.openrdf.repository.sail.config.SailRepositoryConfig;
import org.openrdf.sail.nativerdf.config.NativeStoreConfig;

/**
 * @author pklinov
 *
 */
public class LookIntoRepoUtil {
	
	private static final String PREFIX = 	"PREFIX ad: <http://sswap.info/ad-in-rdf/> \n" + 
			"PREFIX ads: <http://sswap.info/ad-in-rdf/schema/> \n" +
			"PREFIX rdf: <" + RDF.NAMESPACE + "> \n\n";	


	public static void main(String[] args) throws Exception {
		
		LocalRepositoryManager manager = new LocalRepositoryManager(new File("/Users/pklinov/work/UA/j2ee/sswap-module-extractor/test/data/rdf-repo-bot"));
		
    	manager.initialize();
		//manager.addRepositoryConfig(new RepositoryConfig("ad", new SailRepositoryConfig(new NativeStoreConfig())));
		Repository repo = manager.getRepository("map");
		
		RepositoryConnection conn = null;
		
		try {
			conn = repo.getConnection();
			
			String queryString = PREFIX  + "SELECT DISTINCT ?atom ?label ?term FROM " +
					"<http://sswap.info/ad-in-rdf/http://sswapmeet.sswap.info/term_cloud> \n" +  
					"WHERE { \n" +
					"?atom <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ads:Atom . \n" +
					"?atom ads:hasLabel ?label . \n" +
					"?label rdf:type ads:AtomSignatureLabel . \n" +
					"?label ads:hasSignature ?sig . \n" +
					"?sig ads:hasTerm ?term . \n" +
					"}";
			
			System.out.println(queryString);
			
			String queryString2 = "SELECT ?s ?p ?o FROM <http://sswap.info/ad-in-rdf/atom5> WHERE {?s ?p ?o}";
			TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString2);

			// tupleQuery.setBinding("onto_uri",
			// mRepo.getValueFactory().createURI("http://sswap.info/ad-in-rdf/http://www.owllink.org/ontologies/primer"));

			TupleQueryResult result = tupleQuery.evaluate();
			
			try {
				while (result.hasNext()) {
					BindingSet bs = result.next();

					System.out.println(bs);
				}
			} finally {
				result.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				conn.close();	
			}
		}
	}	
}